package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired(required = false)
    private UserInfoService userInfoService;

    @Autowired(required = false)
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    @GetMapping
    public ResponseEntity users(Long userID,
                                @RequestHeader("Authorization") String token) {
        if (userID == null) {
            userID = UserHolder.getId();
        }

        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo,
                                 @RequestHeader("Authorization") String token) {
        Claims claims = JwtUtils.parseToken(token);
        Integer id = (Integer) claims.get("id");
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/header")
    public ResponseEntity updateHead(MultipartFile headPhoto,
                                     @RequestHeader("Authorization") String token) throws IOException {
        Claims claims = JwtUtils.parseToken(token);
        Integer id = (Integer) claims.get("id");
        userInfoService.updateHead(Long.valueOf(id), headPhoto);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }

    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map<String, String> map) {
        String content = map.get("content");
        settingsService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = settingsService.queryBlackList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity removeBlackUser(@PathVariable("uid") Long blackUserId) {
        settingsService.removeBlackUser(blackUserId);
        return ResponseEntity.ok(null);
    }

}
